﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Models;
using System.Web.Routing;
namespace ADShop.Areas.Admin.Controllers
{
    public class OrtherProductController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (Session["Admin"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Oops",
                    action = "Index"
                }));
            }
        }
       // private ADShopEntities db = new ADShopEntities();
        private EShopEntities1 db = new EShopEntities1();

        //
        // GET: /Admin/OrtherProduct/

        public ActionResult Index()
        {
            return View(db.OrderProduct.ToList());
        }

        //
        // GET: /Admin/OrtherProduct/Details/5

        public ActionResult Details(int id = 0)
        {
            OrderProduct orderproduct = db.OrderProduct.Find(id);
            if (orderproduct == null)
            {
                return HttpNotFound();
            }
            return View(orderproduct);
        }

        //
        // GET: /Admin/OrtherProduct/Create

        public ActionResult Create()
        {
            ViewBag.IDShipMethod = new SelectList(db.ShipMethod, "IDShipMethod", "NameShipMethod", 0);
            ViewBag.IDPaymentMethod = new SelectList(db.PaymentMethod, "IDPaymentMethod", "NamePaymentMethod",0);
            ViewBag.EmailRecipient = new SelectList(db.Member, "IDUser", "UserEmail", 0);
            ViewBag.IDUser = new SelectList(db.Member, "IDUser", "IDUser",0);
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", 0);
            return View();
        }

        //
        // POST: /Admin/OrtherProduct/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(OrderProduct orderproduct)
        {
            if (ModelState.IsValid)
            {
                db.OrderProduct.Add(orderproduct);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDShipMethod = new SelectList(db.ShipMethod, "IDShipMethod", "NameShipMethod", orderproduct.IDShipMethod);
            ViewBag.IDPaymentMethod = new SelectList(db.PaymentMethod, "IDPaymentMethod", "NamePaymentMethod", orderproduct.IDPaymentMethod);
            ViewBag.EmailRecipient = new SelectList(db.Member, "IDUser", "UserEmail", orderproduct.EmailRecipient);
            ViewBag.IDUser = new SelectList(db.Member, "IDUser", "IDUser", orderproduct.IDUser);
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", orderproduct.IDProduct);
            return View(orderproduct);
        }

        //
        // GET: /Admin/OrtherProduct/Edit/5

        public ActionResult Edit(int id = 1)
        {
            OrderProduct orderproduct = db.OrderProduct.SingleOrDefault(d => d.IDOrder == id);
            if (orderproduct == null)
            {
                return HttpNotFound();
            }
            ViewBag.IDShipMethod = new SelectList(db.ShipMethod, "IDShipMethod", "NameShipMethod", orderproduct.IDShipMethod);
            ViewBag.IDPaymentMethod = new SelectList(db.PaymentMethod, "IDPaymentMethod", "NamePaymentMethod", orderproduct.IDPaymentMethod);
            ViewBag.EmailRecipient = new SelectList(db.Member, "IDUser", "UserEmail", orderproduct.EmailRecipient);
            ViewBag.IDUser = new SelectList(db.Member, "IDUser", "IDUser", orderproduct.IDUser);
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", orderproduct.IDProduct);
            return View(orderproduct);
        }

        //
        // POST: /Admin/OrtherProduct/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(OrderProduct orderproduct)
        {
            if (ModelState.IsValid)
            {
                db.Entry(orderproduct).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDShipMethod = new SelectList(db.ShipMethod, "IDShipMethod", "NameShipMethod", orderproduct.IDShipMethod);
            ViewBag.IDPaymentMethod = new SelectList(db.PaymentMethod, "IDPaymentMethod", "NamePaymentMethod", orderproduct.IDPaymentMethod);
           // ViewBag.EmailRecipient = new SelectList(db.Member, "EmailRecipient", "UserEmail", orderproduct.EmailRecipient);
            ViewBag.IDUser = new SelectList(db.Member, "IDUser", "IDUser", orderproduct.IDUser);
            ViewBag.IDProduct = new SelectList(db.Product, "IDProduct", "NameProduct", orderproduct.IDProduct);
            return View(orderproduct);
        }

        //
        // GET: /Admin/OrtherProduct/Delete/5

        public ActionResult Delete(int id = 0)
        {
            OrderProduct orderproduct = db.OrderProduct.Find(id);
            if (orderproduct == null)
            {
                return HttpNotFound();
            }
            return View(orderproduct);
        }

        //
        // POST: /Admin/OrtherProduct/Delete/5

      
        public ActionResult DeleteConfirmed(int id)
        {
            OrderProduct orderproduct = db.OrderProduct.Find(id);
            db.OrderProduct.Remove(orderproduct);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}